# -*- coding: utf-8 -*-

import requests
import hashlib
import time
from dingtalkchatbot.chatbot import DingtalkChatbot

url = 'http://61.145.229.28:7902/sms/v2/std/get_balance'
userid = ['JS8619', 'JS8620', 'JS9015', 'JS4855',
          'JS8528', 'JS8529', 'JS8595', 'JS8596']
pwd = ['868125', '868125', '256310', '265210',
       '171328', '171329', '456888', '456777']
# 获取时间戳
timestamp = time.strftime("%m%d%H%M%S", time.localtime())

# 定义列表集
uid_pwd = zip(userid, pwd)

# 定义输出空列表
result_uid = []
result_json = []

# 设置 Webhook 地址
webhook = 'https://oapi.dingtalk.com/robot/send?access_token=c54fa6837d584b0abc56c0a7d0456ca37d59213dcc7ea3f6f104b20de3c71284'
secret = 'SEC9de9f15e7d9bc3be3c0c5f50adcf283ff26189940919486b9cdb0feda9ee45b1'
# 初始化机器人
bot = DingtalkChatbot(webhook=webhook, secret=secret)


def getBlance():
    for uid, upwd in uid_pwd:
        # 明文密码 MD5 加密
        tempPwd = uid.upper() + '00000000' + upwd + timestamp
        md5 = hashlib.md5()
        md5.update(tempPwd.encode('utf-8'))
        md5Pwd = md5.hexdigest()

        # 定义字典
        data = {
            'userid': uid,
            'pwd': md5Pwd,
            'timestamp': timestamp,
        }

        # 获取 json
        response = requests.post(url, data=data)
        result = response.json()
        result_uid.append(uid)
        result_json.append(result)
    return result_uid, result_json


result_uid, result_json = getBlance()

for uid, result in zip(result_uid, result_json):
    if result['result'] == 0:
        if uid == 'JS8619':
            sms_balance_8619 = int(result['balance'])
            msg = '*新浪健康(生产账号) ' + uid + \
                ' 短信余量为：{} 条*.'.format(sms_balance_8619)
            # 将 print 的拼接内容作为消息发送给钉钉
            bot.send_text(msg)
            print(msg)
        if uid == 'JS8620':
            sms_balance_8620 = int(result['balance'])
            msg = '*新浪健康(服务账号) ' + uid + \
                ' 短信余量为：{} 条*.'.format(sms_balance_8620)
            bot.send_text(msg)
            print(msg)
        if uid == 'JS9015':
            sms_balance_9015 = int(result['balance'])
            msg = '*爱问医生 ' + uid + \
                ' 短信余量为：{} 条*.'.format(sms_balance_9015)
            bot.send_text(msg)
            print(msg)
        if uid == 'JS4855':
            sms_balance_4855 = int(result['balance'])
            msg = '*营销类爱问医生 ' + uid + \
                ' 短信余量为：{} 条*.'.format(sms_balance_4855)
            bot.send_text(msg)
            print(msg)
        if uid == 'JS8528':
            sms_balance_8528 = int(result['balance'])
            msg = '*爱问健康 ' + uid + \
                ' 短信余量为：{} 条*.'.format(sms_balance_8528)
            bot.send_text(msg)
            print(msg)
        if uid == 'JS8529':
            sms_balance_8529 = int(result['balance'])
            msg = '*营销类爱问健康 ' + uid + \
                ' 短信余量为：{} 条*.'.format(sms_balance_8529)
            bot.send_text(msg)
            print(msg)
        if uid == 'JS8595':
            sms_balance_8595 = int(result['balance'])
            msg = '*爱问社区 ' + uid + \
                ' 短信余量为：{} 条*.'.format(sms_balance_8595)
            bot.send_text(msg)
            print(msg)
        if uid == 'JS8596':
            sms_balance_8596 = int(result['balance'])
            msg = '*营销类爱问社区 ' + uid + \
                ' 短信余量为：{} 条*.'.format(sms_balance_8596)
            bot.send_text(msg)
            print(msg)
    else:
        msg = '* ' + uid + ' 查询失败，错误码为：{}*'.format(result['result'])
        bot.send_text(msg)

sum_blance = (sms_balance_8619, sms_balance_8620, sms_balance_9015, sms_balance_4855,
              sms_balance_8528, sms_balance_8529, sms_balance_8595, sms_balance_8596)
msg = '*总短信余量为：{} 条*.'.format(sum(sum_blance))
bot.send_text(msg)
print(msg)

